﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Spring.Context;
using Spring.Context.Support;
using ESPlus.Core;
using ESPlus.Application.Basic.Passive;
using ESPlus.Application.Group.Passive;
using ESBasic.ObjectManagement;
using ESPlus.Core.Passive;
using ESPlus.Application.Chat.Passive;



namespace ESFDemo
{
    static class Program
    {
        public static IApplicationContext SpringContext;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Program.SpringContext = ContextRegistry.GetContext();
            Program.ConfigEsbLogger();
            IBasicOutter basicOutter = (IBasicOutter)Program.SpringContext.GetObject("basicOutter");
            LoginForm loginForm = new LoginForm(basicOutter);
            if (loginForm.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            Program.ConfigIOutter(loginForm.UserID);            
            MainForm mainForm = (MainForm)Program.SpringContext.GetObject("mainForm");
            mainForm.Initialize(loginForm.UserID);
            Application.Run(mainForm);
            //Application.Run(new Form1());
        }

        #region ConfigIOutter
        private static void ConfigIOutter(string curUserID)
        {
            string[] names = Program.SpringContext.GetObjectDefinitionNames();
            foreach (string name in names)
            {
                object obj = Program.SpringContext.GetObject(name);
                if (obj.GetType() == typeof(MarshalByRefObject))
                {
                    continue;
                }

                IOutter outter = obj as IOutter;
                if (outter != null)
                {
                    outter.Initialize(curUserID);
                }
            }
        }
        #endregion

        #region ConfigEsbLogger
        private static void ConfigEsbLogger()
        {
            IList<object> objList = new List<object>();
            string[] names = Program.SpringContext.GetObjectDefinitionNames();
            foreach (string name in names)
            {
                objList.Add(Program.SpringContext.GetObject(name));
            }

            object esfLogger = Program.SpringContext.GetObject("esfLogger");
            ESBasic.Helpers.ReflectionHelper.SetProperty(objList, "EsfLogger", esfLogger);
        }
        #endregion
    }
}
